package com.itheima.pinda.file.strategy;

import cn.hutool.core.util.StrUtil;
import com.itheima.pinda.file.domain.FileDeleteDO;
import com.itheima.pinda.file.entity.File;
import com.itheima.pinda.file.properties.FileServerProperties;
import com.itheima.pinda.file.strategy.impl.AbstractFileStrategy;
import com.itheima.pinda.utils.DateUtils;
import com.itheima.pinda.utils.StrPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @description: 本地上传策略配置类
 * @author: 余希瑶
 * @date: 2021年06月30日 16:03
 * @version:1.0
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(FileServerProperties.class)
//配置类什么时候生效
@ConditionalOnProperty(name="pinda.file.type",havingValue = "LOCAL")
public class LocalAutoConfigure {

    /**
     * 本地文件策略处理类
     */
    @Service
    public class LocalServiceImpl extends AbstractFileStrategy{

        private void buildClient(){
             properties = fileProperties.getLocal();
        }

        /**
         *  文件上传
         * @param file
         * @param multipartFile
         * @return
         */
        @Override
        public void uploadFile(File file, MultipartFile multipartFile) throws Exception{
            buildClient();
            String endpoint = properties.getEndpoint();
            String bucketName = properties.getBucketName();
            String uriPrefix = properties.getUriPrefix();

            //使用UUID为文件生成新文件名
            String fileName = UUID.randomUUID().toString()+"."+file.getExt();
            //日期文件夹，例如：2020\04
            String relativePath = Paths.get(LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_MONTH_FORMAT_SLASH))).toString();
            // web服务器存放的绝对路径，例如：D:\\uploadFiles\\oss-file-service\\2020\\04
            String absolutePath = Paths.get(properties.getEndpoint(), properties.getBucketName(), relativePath).toString();


            //目标输出文件
            java.io.File outFile = new java.io.File(Paths.get(absolutePath, fileName).toString());

            //向目标文件写入数据
            FileUtils.writeByteArrayToFile(outFile,multipartFile.getBytes());

            //文件上传完成后需要设置File对象的属性(url,fileName,relativePath),用户保存到数据库
            String url = new StringBuilder(getUriPrefix())
                    .append(StrPool.SLASH)
                    .append(properties.getBucketName())
                    .append(StrPool.SLASH)
                    .append(relativePath)
                    .append(StrPool.SLASH)
                    .append(fileName)
                    .toString();
            //替换掉windows环境的\路径
            url = StrUtil.replace(url, "\\\\", StrPool.SLASH);
            url = StrUtil.replace(url, "\\", StrPool.SLASH);
            file.setUrl(url);
            file.setFilename(fileName);
            file.setRelativePath(relativePath);

        }

        /**
         * 文件删除
         * @param fileDeleteDO
         */
        @Override
        public void delete(FileDeleteDO fileDeleteDO) {
            //拼接要删除的文件的绝对磁盘路径

            java.io.File ioFile =
                    new java.io.File(Paths.get(properties.getEndpoint(),
                            properties.getBucketName(),
                            fileDeleteDO.getRelativePath(),
                            fileDeleteDO.getFileName()).toString());

            org.apache.commons.io.FileUtils.deleteQuietly(ioFile);
        }
    }
}
